const express = require("express");

const router = express.Router();
const articleServ = require("../../services/articleService");
const { asyncHandle } = require("../getSendResult");
//分页获取列表
router.get(
  "/",
  asyncHandle(async (req, res) => {
    const page = req.query.page || 1;
    const limit = req.query.limit || 10;
    const search = req.query.search;
    const typeId = req.query.typeId
    const isRecommend = req.query.isRecommend
    const sort = req.query.sort
    return await articleServ.findArticles(page, limit,typeId, search,isRecommend,sort);
  })
);

//获取单个
router.get("/:id", asyncHandle(async (req,res)=>{
    return await articleServ.findArticleById(req.params.id)
}));

//新增一个
router.post("/", asyncHandle(async (req,res)=>{
    return await articleServ.addArticle(req.body)
}));

//修改
router.put("/:id", asyncHandle(async (req,res)=>{
    return await articleServ.updateArticle(req.params.id,req.body)
}));

//删除
router.delete("/:id", asyncHandle(async (req,res)=>{
    return await articleServ.deleteArticle(req.params.id)
}));
//增加一个预览数
router.get("/view/:id",asyncHandle(async (req,res)=>{
  return await articleServ.incrementView(req.params.id,req.query.count)
}))
// 推荐文章
router.post("/recommend/:id",asyncHandle(async (req,res)=>{
  return await articleServ.recommend(req.params.id,req.body)
}))
module.exports = router;
